import Vue from 'vue'

export default class Utils {
  /**
   * 判断字符串是否为空
   * @param str
   * @returns {boolean}
   */
  static isNull (str) {
    return str == null || str.length === 0 || str === ''
  }

  /**
   *
   * @desc  判断是否为身份证号
   * @param  {String|Number} str
   * @return {Boolean}
   */
  static isIdCard (str) {
    return /^(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$)$/.test(str)
  }

  /**
   *
   * @desc   判断是否为手机号
   * @param  {String|Number} str
   * @return {Boolean}
   */
  static isPhoneNum (str) {
    return /^(0|86|17951)?(1[3-9][0-9])[0-9]{8}$/.test(str)
  }

  static getTagType4Circle (circleStatus) {
    var tagClassType
    switch (circleStatus) {
      case 1:
        tagClassType = 'danger'
        break
      case 2:
        tagClassType = 'info'
        break
      case 3:
        tagClassType = 'warning'
        break
      case 4:
        tagClassType = 'danger'
        break
      case 5:
        tagClassType = ''
        break
    }
    return tagClassType
  }

  static getBase64FileLength (base64) {
    if (!base64) return 0
    var str = base64.substring(22)
    var equalIndex = str.indexOf('=')
    if (equalIndex > 0) {
      str = str.substring(0, equalIndex)
    }
    var strLength = str.length
    var fileLength = parseInt(strLength - (strLength / 8) * 2)
    return fileLength
  }

  static isvalidPhone (str) {
    const reg = /^1[3|4|5|7|8][0-9]\d{8}$/
    return reg.test(str)
  }

  // 生成从minNum到maxNum的随机数
  static randomNum (minNum, maxNum) {
    switch (arguments.length) {
      case 1:
        return parseInt(Math.random() * minNum + 1, 10)
      case 2:
        return parseInt(Math.random() * (maxNum - minNum + 1) + minNum, 10)
      default:
        return 0
    }
  }

  // 获取浏览器语言设置
  // 根据业务：浏览器设置非中文，又非英文时，统一返回英文
  static getBrowserLang () {
    let self = this
    let JsSrc = (navigator.language || navigator.browserLanguage).toLowerCase()

    if (JsSrc.indexOf('zh-cn') >= 0 || JsSrc.indexOf('zh') >= 0) {
      // 假如浏览器语言是中文
      return 'zh-cn'
    } else if (JsSrc.indexOf('en') >= 0) {
      // 假如浏览器语言是英文
      return 'en'
    }
    // 浏览器设置非中文，又非英文时，统一返回英文
    return 'en'
  }

  /**
   * 时间日期格式化
   * @param format
   * @returns {*}
   */
  static dateFormat (dateObj, format) {
    let date = {
      'y+': dateObj.getFullYear(),
      'M+': dateObj.getMonth() + 1,
      'd+': dateObj.getDate(),
      'h+': dateObj.getHours(),
      'm+': dateObj.getMinutes(),
      's+': dateObj.getSeconds(),
      'q+': Math.floor((dateObj.getMonth() + 3) / 3),
      'S+': dateObj.getMilliseconds()
    }
    if (/(y+)/i.test(format)) {
      format = format.replace(RegExp.$1, (dateObj.getFullYear() + '').substr(4 - RegExp.$1.length))
    }
    for (let k in date) {
      if (new RegExp('(' + k + ')').test(format)) {
        format = format.replace(RegExp.$1, RegExp.$1.length === 1
          ? date[k] : ('00' + date[k]).substr(('' + date[k]).length))
      }
    }
    return format
  }

  /**
   时间戳转日期格式（YYYY-MM-dd HH:mm:ss）
   */
  static formatUnixtimestamp (unixtimestamp) {
    var date = new Date(unixtimestamp)
    var y = date.getFullYear()
    var m = date.getMonth() + 1
    m = m < 10 ? ('0' + m) : m
    var d = date.getDate()
    d = d < 10 ? ('0' + d) : d
    var h = date.getHours()
    h = h < 10 ? ('0' + h) : h
    var minute = date.getMinutes()
    var second = date.getSeconds()
    minute = minute < 10 ? ('0' + minute) : minute
    second = second < 10 ? ('0' + second) : second
    return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second

  }
}
